﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/JobIntention")]
    public class JobIntentionController : ApiController
    {

        B_JobIntention _job;
        public JobIntentionController()
        {
            _job = new B_JobIntention();
        }

        /// <summary>
        /// 获取模板求职意向数据（测试专用）
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData()
        {
            B_Template a = new B_Template();
            var data = from i in _job.GetAll().ToList()
                       join p in a.GetAll().ToList()
                       on i.TemplateID equals p.TemplateID
                       orderby i.JobID ascending
                       select new Param_JobIntention
                       {
                           JobID = i.JobID,
                           JobName = i.JobName,
                           JobCity = i.JobCity,
                           JobSalary = i.JobSalary,
                           EntryTime = i.EntryTime,
                           TemplateID = i.TemplateID,
                           Tname = p.Tname,
                       };
            return Ok(new Result<List<Param_JobIntention>>
            {
                Data = data.ToList(),
            });
        }

        /// <summary>
        /// 获取模板求职意向数据
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData(int page, int size)
        {
            B_Template a = new B_Template();
            var data = from i in _job.GetAll().ToList()
                       join p in a.GetAll().ToList()
                       on i.TemplateID equals p.TemplateID
                       orderby i.JobID ascending
                       select new Param_JobIntention
                       {
                           JobID = i.JobID,
                           JobName = i.JobName,
                           JobCity = i.JobCity,
                           JobSalary = i.JobSalary,
                           EntryTime = i.EntryTime,
                           TemplateID = i.TemplateID,
                           Tname = p.Tname,
                       };
            return Ok(new Result<List<Param_JobIntention>> {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpPost]
        public IHttpActionResult Delete(int id)
        {
            var data = _job.GetAll().Where(x => x.JobID == id);
            if (data.Count() > 0)
            {
                _job.Delete(id);
                return Ok(new Result<object> {});
            }
            else
            {
                return Ok(new Result<object> { Msg = "不存在" });
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpGet]
        public IHttpActionResult Add(Param_JobIntention param)
        {
            var data = new JobIntention
            {
                JobName = param.JobName,
                JobCity = param.JobCity,
                JobSalary = param.JobSalary,
                EntryTime = param.EntryTime,
                TemplateID = param.TemplateID
            };
            _job.Add(data);
            return Ok(new Result<object> { });
        }

        /// <summary>
        /// 根据模板ID查询求职意向
        /// </summary>
        /// <returns></returns>
        [Route("FindByTemplateID/{id}")]
        [HttpGet]
        public IHttpActionResult FindByTemplateID(int id)
        {

            var data = _job.FindByTemplateID(id);
            if (data==null)
            {
                return Ok(new Result<object>
                {
                    Code=404,
                    Msg="数据为空"
                });
            }
            else
            {
                return Ok(new Result<IDAL.Param_JobIntention>
                {
                    Data = data
                });
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("FindByID/{id}")]
        [HttpGet]
        public IHttpActionResult FindByID(int id)
        {
            var data = _job.FindById(id);
            if (data != null)
            {
                return Ok(new Result<JobIntention>
                {
                    Data = new JobIntention
                    {
                        JobName = data.JobName,
                        JobCity = data.JobCity,
                        JobSalary = data.JobSalary,
                        EntryTime = data.EntryTime,
                        TemplateID = data.TemplateID
                    }
                });
            }
            else
            {
                return Ok(new Result<object> { Msg = "不存在", Code = 404 });
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, JobIntention param)
        {
            try
            {
                if (_job.FindById(id) == null)
                {
                    return Ok(new Result<object> { Msg = "不存在", Code = 404 });
                }
                _job.Update(id, param);
                return Ok(new Result<object>());
            }
            catch (Exception ex)
            {
                return Ok(new Result<object> { Msg = ex.Message, Code = 500 });
            }
            
        }
    }
}
